package com.handinhand.mobile.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.handinhand.mobile.HandInHandApplication;
import com.handinhand.mobile.conn.ConnectionUtils;
import com.handinhand.mobile.conn.HandInHandCallback;
import com.handinhand.mobile.conn.HandInHandTask;
import com.handinhand.mobile.exception.HandInHandException;
import com.handinhand.mobile.model.Community;
import com.handinhand.mobile.model.User;
import com.handinhand.mobile.utils.Constants;

public class CommunityService {
	private HandInHandApplication app;
	
	public CommunityService(HandInHandApplication app) {
		this.app = app;
	}
	public ArrayList<Community> getCommunities(Long userId) throws HandInHandException{
		ArrayList<Community> communities = new ArrayList<Community>();
		
		String url = Constants.SERVER + "mobile/signup";
		JSONObject json = new JSONObject();
		
		try {
			json.put("userId", userId);
		} catch (JSONException e) {
			throw new HandInHandException("An unknown error occured!");
		}

		Map<String, String> map = new HashMap<String, String>();
		JSONObject jsonObject = ConnectionUtils.makePostRequest(url, map, json, app.getAuthToken());
		
		if (jsonObject.has("communityList")) {
			try {
				JSONArray communitiesJson = jsonObject.getJSONArray("communityList");

				for (int i = 0; i < communitiesJson.length(); i++) {
					communities.add(convertJsonToCommunity(communitiesJson.getJSONObject(i)));
				}
			} catch (JSONException e) {
				throw new HandInHandException("An error occured while parsing json returned from the server!");
			}
		} else {
			throw new HandInHandException("An unknown error occured while creating the community :(");
		}
		
		return communities;
	}
	
	/**
	 * Make  post request to create community 
	 * @param name is name of community
	 * @param description is description for community
	 * @param callback passed as parameter to HandInHandTask
	 */
	public void createCommunity(final String name, final String description,  HandInHandCallback<Community> callback) {
		HandInHandTask<Community> communityTask = new HandInHandTask<Community>(callback) {
			@Override
			public Community run() throws HandInHandException {
				String url = Constants.SERVER + "mobile/newCommunity";

				JSONObject json = new JSONObject();
				try {
					json.put("name", name);
					json.put("description", description);					
				} catch (JSONException e) {
					throw new HandInHandException("An unknown error occured!");
				}

				Map<String, String> map = new HashMap<String, String>();
				JSONObject jsonObject = ConnectionUtils.makePostRequest(url, map, json, app.getAuthToken());
				
				
				try {
					return convertJsonToCommunity(jsonObject);
				} catch (JSONException e) {
					throw new HandInHandException("JSON returned from server is invalid!");
				}
				
			}
		};
		HandInHandTask.execute(communityTask);
	}
	
	private Community convertJsonToCommunity(JSONObject json) throws JSONException {
		Community community = new Community();

		if (json.has("name")) {
			community.setName(json.getString("name"));
		}

		if (json.has("description")) {
			community.setDescription(json.getString("description"));
		}

		if (json.has("id")) {
			community.setId(json.getLong("id"));
		}

		return community;
	}
}
